package com.campus.client.view;

import com.campus.common.vo.User;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class TeacherCourseFrame {

    private Button btnLogout;

    private User user=new User();
    public User getUser() {return user;}
    public void setUser(String Id,String password,String role) {
        this.user.setId(Id);
        this.user.setPassword(password);
        this.user.setRole(role);
        //System.out.print("用户名"+this.user.getId());
    }


    public void start(Stage primaryStage) {
        // 主布局
        BorderPane root = new BorderPane();

        // 校徽
        ImageView logoView = new ImageView(new Image(getClass().getResource("/badge.png").toExternalForm()));
        logoView.setFitHeight(40);
        logoView.setPreserveRatio(true);

        // 中文标题
        Label lblTitleCN = new Label("选课");
        lblTitleCN.setStyle("-fx-text-fill: white; -fx-font-size: 18px; -fx-font-weight: bold;");

        // 英文标题
        Label lblTitleEN = new Label("Course Selection");
        lblTitleEN.setStyle("-fx-text-fill: white; -fx-font-size: 12px;");

        // 垂直堆叠：中文在上，英文在下
        VBox titleBox = new VBox(2, lblTitleCN, lblTitleEN);
        titleBox.setAlignment(Pos.CENTER_LEFT);

        // 把校徽和标题组合在一起
        HBox logoAndTitle = new HBox(8, logoView, titleBox);
        logoAndTitle.setAlignment(Pos.CENTER_LEFT);

        // 中间撑开的空白
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);

        // 身份标签
        Label lblRole = new Label("当前身份：教师");
        lblRole.setStyle("-fx-text-fill: white; -fx-font-size: 12px; -fx-font-weight: bold;");

        // 退出按钮
        // 加载图标
        Image logoutImg = new Image(getClass().getResource("/logout.png").toExternalForm());
        ImageView logoutIcon = new ImageView(logoutImg);
        logoutIcon.setFitHeight(14);   // 控制大小
        logoutIcon.setPreserveRatio(true);

        // 按钮
        btnLogout = new Button("退出选课", logoutIcon);
        btnLogout.getStyleClass().add("logout-button");
        btnLogout.setOnAction(e -> {
            try
            {
                back();
            }
            catch (IOException ex) {
                throw new RuntimeException
                        (ex);
            }
        });

        // 顶部工具栏
        HBox topBar = new HBox(15, logoAndTitle, spacer, lblRole, btnLogout);
        topBar.setPadding(new Insets(6));
        topBar.setStyle("-fx-background-color: #3b7bc3;");
        topBar.setAlignment(Pos.CENTER_LEFT);

        // 左侧侧边栏
        VBox sidebar = new VBox(10);
        sidebar.setPadding(new Insets(15, 0, 15, 0)); // 上 右 下 左
        sidebar.setStyle("-fx-background-color: #3b7bc3");

        SidebarButton btnAllCourses = new SidebarButton("所有课程");
        SidebarButton btnMyCourses = new SidebarButton("我的课程");
        sidebar.getChildren().addAll(btnAllCourses, btnMyCourses);


        // 中间内容区域
        BorderPane contentArea = new BorderPane();

        // 顶部搜索栏
        HBox searchBar = new HBox(10);
        searchBar.setPadding(new Insets(10));
        searchBar.setAlignment(Pos.CENTER_LEFT);

        TextField searchField = new TextField();
        searchField.setPromptText("输入课程编号或名称");
        searchField.setPrefWidth(300);

        Button searchBtn = new Button("🔍 查询");
        searchBtn.setStyle("-fx-background-color: #3b7bc3; -fx-text-fill: white; -fx-font-weight: bold;");
        searchBtn.setOnAction(e -> {
            String keyword = searchField.getText().trim();
            if (keyword.isEmpty()) {
                // 默认查询所有课程
                TeacherCourseView cv = new TeacherCourseView(TeacherCourseView.CourseViewMode.ALL, user);
                contentArea.setCenter(cv.getView());
            } else {
                // 按关键字查询
                TeacherCourseView cv = new TeacherCourseView(TeacherCourseView.CourseViewMode.QUERY, keyword, user);
                contentArea.setCenter(cv.getView());
            }
        });

        searchBar.getChildren().addAll(searchField, searchBtn);

        contentArea.setTop(searchBar);


        TeacherCourseView defaultView = new TeacherCourseView(TeacherCourseView.CourseViewMode.ALL, user);
        contentArea.setCenter(defaultView.getView());


        List<SidebarButton> sidebarButtons = Arrays.asList(btnAllCourses, btnMyCourses);
        updateSidebarSelection(sidebarButtons, btnAllCourses);
        // 点击“可选课程”按钮 → 加载 CourseView
        btnAllCourses.setOnAction(e -> {
            TeacherCourseView cv = new TeacherCourseView(TeacherCourseView.CourseViewMode.ALL, user);
            contentArea.setCenter(cv.getView()); // 嵌入课程表格
            updateSidebarSelection(sidebarButtons, btnAllCourses);
        });

        // 点击“已选课程”按钮（先占位）
        btnMyCourses.setOnAction(e -> {
            TeacherCourseView cv = new TeacherCourseView(TeacherCourseView.CourseViewMode.MYCOURSES, user);
            contentArea.setCenter(cv.getView());
            updateSidebarSelection(sidebarButtons, btnMyCourses);
        });

        // 布局放入 BorderPane
        root.setTop(topBar);
        root.setLeft(sidebar);
        root.setCenter(contentArea);

        // 场景
        Scene scene = new Scene(root, 900, 600);
        scene.getStylesheets().add(getClass().getResource("/CourseView.css").toExternalForm());

        // 舞台
        primaryStage.setTitle("教师端课程管理系统 - 简易版");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void back() throws IOException {
        // 获取当前登录窗口的Stage
        Stage libraryStage = (Stage) btnLogout.getScene().getWindow();

        TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
        teacherMainFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Stage stage = new Stage();
        teacherMainFrame.show(stage);

        libraryStage.close();
    }
    private void updateSidebarSelection(List<SidebarButton> buttons, SidebarButton selected)
    {
        for (SidebarButton btn : buttons) {
            btn.setSelected(false);
        }
        selected.setSelected(true);
    }

}
